<?php

namespace app\admin\validate;

use think\Validate;
use app\common\Error;

class Black extends Validate
{
    protected $rule = [
        'limit' => 'between:0,100',
        'start_date' => 'date|dateFormat:Y-m-d|checkStartDate',
        'end_date' => 'date|dateFormat:Y-m-d|checkEndDate',
        'uid' => 'require',
        'reason' => 'require'
    ];

    protected $message = [
        'start_date.date' => '开始日期不是有效日期',
        'end_date.date' => '结束日期不是有效日期',
        'start_date.dateFormat' => '开始日期必须是Y-m-d格式',
        'end_date.dateFormat' => '结束日期必须是Y-m-d格式',
        'limit.between' => '内容条数只能在1-100之间',
        'uid.require' => 'uid参数不能为空',
        'reason.require' => 'reason参数不能为空'
    ];

    protected $scene = [
        'user_black_list' => ['limit', 'start_date', 'end_date'],
        'add_user_black' => ['uid', 'reason'],
        'cancel_user_black' => ['uid']
    ];

    protected function checkStartDate($value, $rule, $data = [])
    {
        $today = strtotime(date('Y-m-d'));
        if (strtotime($value) > $today) {
            return '开始日期不能超过今天';
        }
        return true;
    }

    protected function checkEndDate($value, $rule, $data = [])
    {
        $endDate = strtotime($data['end_date']);
        $correctEndDate = strtotime('+1 month', strtotime($data['start_date']));
        if ($endDate > $correctEndDate) {
            return '结束日期不能大于开始日期一个月';
        }
        return true;
    }
}
